#pragma once
#include<assert.h>
namespace refrain
{
	template<class T>
	struct list_node
	{
		list_node* _prev;
		list_node* _next;
		T data;
		list_node(const T& x = T())
			: _prev(nullptr)
			, _next(nullptr)
			, data(x)
		{}
	};

	template<class Iterator, class Ref, class Ptr>
	struct Reverse_iterator
	{
		typedef Reverse_iterator<Iterator, Ref, Ptr> Self;

		Iterator _it;

		Reverse_iterator(Iterator it) 
			: _it(it) 
		{}
		Self& operator++()
		{
			_it--;
			return *this;
		}
		Self operator++(int)
		{
			Self tmp(*this);
			--*this;
			return tmp;
		}
		Self& operator--()
		{
			_it++;
			return *this;

		}
		Self operator--(int)
		{
			Self tmp(*this);
			++*this;
			return tmp;
		}
		Ptr operator->()
		{
			return &(operator*());
		}
		Ref operator*()
		{
			Iterator tmp = _it;
			--tmp;
			return *tmp;
		}
		bool operator==(const Self& x) const
		{
			return _it == x;
		}
		bool operator!=(const Self& x) const
		{
			return _it != x;
		}
	};
	template<class T, class Ref, class Ptr>
	struct list_iterator
	{
		typedef list_node<T> Node;
		typedef list_iterator<T, Ref, Ptr> Self;

		Node* _node;
		list_iterator(Node* node)
			: _node(node)
		{}
		Ptr operator->()
		{
			return &_node->data;
		}
		Ref operator*()
		{
			return _node->data;
		}
		Self& operator++()
		{
			_node = _node->_next;
			return *this;
		}
		Self operator++(int)
		{
			Self tmp(*this);
			++*this;
			return tmp;
		}
		Self& operator--()
		{
			_node = _node->_prev;
			return *this;
		}
		Self operator--(int)
		{
			Self tmp(*this);
			--*this;
			return tmp;
		}
		bool operator==(const Self& x) const 
		{
			return _node == x.node; 
		}
		bool operator!=(const Self& x) const 
		{
			return _node != x._node; 
		}
	};


	template<class T>
	class list
	{
		typedef list_node<T> Node;
	public:
		typedef list_iterator<T, T&, T*> iterator;
		typedef list_iterator<T, const T&, const T*> const_iterator;
		typedef Reverse_iterator<iterator, T&, T*> reverse_iterator;
		typedef Reverse_iterator<iterator, const T&, const T*> const_reverse_iterator;

		reverse_iterator rbegin()
		{
			return _head->_prev;
		}
		reverse_iterator rend()
		{
			return _head;
		}
		const_reverse_iterator rbegin() const
		{
			return _head->_prev;
		}
		const_reverse_iterator rend() const
		{
			return _head;
		}

		iterator begin()
		{
			return _head->_next;
		}

		iterator end()
		{
			return _head;
		}
		const_iterator begin() const
		{
			return _head->_next;
		}

		const_iterator end() const
		{
			return _head;
		}



		void empty_initialize()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
		}
		list()
		{
			empty_initialize();
		}
		list(initializer_list<T> il)
		{
			empty_initialize();
			for (auto& e : il)
			{
				push_back(e);
			}
		}
		list<T>& operator=(list<T> lt)
		{
			swap(lt);
			return *this;
		}

		void swap(list<T>& lt)
		{
			std::swap(_head, lt->_head);
			std::swap(_size, lt._size);
		}
		list(list<T>& lt)
		{
			empty_initialize();
			
			for (auto & e : lt)
			{
				push_back(e);
			}
		}

		void clear()
		{
			auto it = begin();
			while (it != end())
			{
				it = erase(it);
			}
		}
		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}

		size_t size()
		{
			return _size;
		}

		void push_back(const T& x)
		{
			// tail newnode _head
			/*Node* tail = _head->_prev;
			Node* newnode = new Node(x);
			
			tail->_next = newnode;
			
			newnode->_prev = tail;
			newnode->_next = _head;

			_head->_prev = newnode;
			_size++;*/
			insert(end(), x);
		}
		void push_front(const T& x)
		{
			insert(begin(), x);
		}
		void pop_front()
		{
			erase(begin());
		}
		void pop_back()
		{
			erase(--end());
		}
		void insert(iterator pos, const T& x)
		{
			//prev newnode cur
			Node* cur = pos._node;
			Node* newnode = new Node(x);
			Node* prev = cur->_prev;

			prev->_next = newnode;
			newnode->_prev = prev;
			newnode->_next = cur;
			cur->_prev = newnode;
			_size++;
		}
		iterator erase(iterator pos)
		{
			assert(pos != end());
			//prev del next
			Node* del = pos._node;
			Node* prev = del->_prev;
			Node* next = del->_next;

			prev->_next = next;
			next->_prev = prev;

			delete del;
			--_size;
			return next;
			//return iterator(next);
		}
	private:
		Node* _head;
		size_t _size;
	};
	void print(const list<int>& lt)
	{
		for (auto x : lt)
		{
			cout << x << " ";
		}
		cout << endl;
	}
	void test_list01()
	{
		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);
		lt.push_back(5);
	
		auto it = lt.begin();
		while (it != lt.end())
		{
			cout << *it << " ";
		
			it++;
		}
		cout << endl;
		

	}
	void test_list02()
	{
		list<int> lt = {2,3,4,64,4};
		print(lt);
	}
}